package com.zly.bscloud.service.impl;

import com.zly.bscloud.dto.OrderdetailDTO;
import com.zly.bscloud.entity.Orderdetail;
import com.zly.bscloud.mapper.OrderdetailMapper;
import com.zly.bscloud.service.OrderdetailService;
import com.zly.bscloud.utils.entity.StatusCode;
import com.zly.bscloud.utils.exception.BpException;
import com.zly.bscloud.utils.utils.BeanHelper;
import com.zly.bscloud.utils.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author zhouxiaoyu
 * @Date 2020/5/27 0:47
 * @Version 1.0
 */
@Service
public class OrderdetailServiceImpl implements OrderdetailService {
    @Autowired
    private OrderdetailMapper orderdetailMapper;

    @Override
    @Transactional
    public Orderdetail saveOrderdetail(Orderdetail orderdetail){
        orderdetail.setId(UUIDUtils.UUID());
        orderdetail.setWeight(0.0);
        orderdetail.setPrice(0.0);

        //插入进数据库
        int count = orderdetailMapper.insert(orderdetail);
        //只要不为1 则操作失败 抛出异常
        if (count != 1)
            throw new BpException(StatusCode.Failure);
        return orderdetail;
    }

    @Override
    public List<OrderdetailDTO> getOrderdetail(String order_id){
        // 创建Example
        Example example = new Example(Orderdetail.class);
        // 创建Criteria
        Example.Criteria criteria = example.createCriteria();
        // 添加条件
        criteria.andEqualTo("order_id",order_id);

        List<Orderdetail> orderdetailList = orderdetailMapper.selectByExample(example);

        List<OrderdetailDTO> orderdetailDTOList = BeanHelper.copyWithCollection(orderdetailList, OrderdetailDTO.class);
        return orderdetailDTOList;
    }
}
